package com.zhidisoft.system.springmvccontroller;

import com.google.code.kaptcha.Constants;
import com.zhidisoft.manage.dao.TaxPayerDao;
import com.zhidisoft.system.dao.TaxerDao;
import com.zhidisoft.system.dao.UserDao;
import com.zhidisoft.system.entity.Taxer;
import com.zhidisoft.system.entity.User;
import com.zhidisoft.util.EncryptUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class SystemSpringMvc {

    /**
     * 用户登录验证
     *
     * @param user
     * @param remUser
     * @param session
     * @param resp
     * @return
     */
    @RequestMapping("/login.do")
    @ResponseBody
    public ModelMap login(User user, String remUser, HttpSession session, HttpServletResponse resp) {

        String username = user.getUsername();
        String password = user.getPassword();

        ModelMap map = new ModelMap();

        String s = EncryptUtil.encryptMD5(password);
        User user1 = UserDao.selectName(username, s);
        Taxer taxer = TaxerDao.getTaxer(username);

        TaxPayerDao tpd = new TaxPayerDao();
        int count = tpd.count();
        session.setAttribute("count", count);

        if (user1 != null) {
            if ("on".equals(remUser)) {
                Cookie cookie = new Cookie("username", username);
                resp.addCookie(cookie);
                map.put("success", true);
            }
            map.put("success", true);
            map.put("msg", "");
            session.setAttribute("user", user1);
            session.setAttribute("username", username);
            if (taxer != null) {
                session.setAttribute("taxer", taxer);
            }
        } else {
            map.put("success", false);
            map.put("msg", "用户名或者密码错误");
        }
        return map;
    }

    /**
     * 验证码验证
     *
     * @param captcha
     * @param session
     * @return
     */
    @RequestMapping("/checkCaptcha.do")
    @ResponseBody
    public ModelMap checkCaptcha(String captcha, HttpSession session) {
        ModelMap map = new ModelMap();
        String code = session.getAttribute(Constants.KAPTCHA_SESSION_KEY).toString();
        if (captcha.equals(code)) {
            map.put("success", true);
            map.put("msg", "");
        } else {
            map.put("success", false);
            map.put("msg", "请输入正确的验证码");
        }
        return map;
    }


    /**
     * 跳转主页面
     *
     * @return
     */
    @RequestMapping("/index.do")
    public String index() {
        return "index";
    }

    /**
     * 跳转首页
     *
     * @return
     */
    @RequestMapping("/home.do")
    public String home() {
        return "home";
    }

    /**
     * 跳转用户信息页面
     *
     * @return
     */
    @RequestMapping("/toUserInfo.do")
    public String toUserInfo() {
        return "userInfo";
    }

    /**
     * 跳转修改密码页面
     *
     * @return
     */
    @RequestMapping("/toModifypassword.do")
    public String toModifypassword() {
        return "modifypassword";
    }

    /**
     * 修改密码
     *
     * @param oldPassword
     * @param newPassword
     * @param username
     * @return
     */
    @RequestMapping("/updatePassword.do")
    @ResponseBody
    public ModelMap updatePassword(String oldPassword, String newPassword, String username) {
        ModelMap modelMap = new ModelMap();
        boolean b = UserDao.update(username, oldPassword, newPassword);
        if (b) {
            modelMap.put("success", true);
            modelMap.put("msg", "修改成功");
        } else {
            modelMap.put("success", false);
            modelMap.put("msg", "修改失败，密码输入错误");
        }
        return modelMap;
    }


}
